﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FriendlyFireLibrary;

namespace NetworkTransfer
{
    public partial class Server2 : Form
    {
        public Server2()
        {
            InitializeComponent();
        }

        NetworkAsyncCodeServer server;
        private void MyFunc(byte[] data, int type)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            server = new NetworkAsyncCodeServer(new NetworkCodeCommon.DataReceived(MyFunc), txtServer, lsbClients);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
        }

        private void button5_Click(object sender, EventArgs e)
        {
            
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (lsbClients.SelectedIndex > -1)
            {
                for(int i = 0; i < lsbClients.SelectedIndices.Count; i++)
                    server.SendMessage(txtMessage.Text, server.clients[lsbClients.SelectedIndices[i]]);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            server.Shutdown();
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
        }
        private void button5_Click_1(object sender, EventArgs e)
        {
            if (lsbClients.SelectedIndex > -1)
            {
                openFileDialog1.ShowDialog();
                txtServer.Text += "Server Sending File..." + Environment.NewLine;
                server.SendFile(openFileDialog1.FileName, server.clients[lsbClients.SelectedIndex]);
                txtServer.Text += "Server Sent File : " + openFileDialog1.FileName + Environment.NewLine;
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void btnRequest_Click(object sender, EventArgs e)
        {
            if (lsbClients.SelectedIndex > -1)
            {
                server.RequestFile(txtRequestFile.Text, server.clients[lsbClients.SelectedIndex]);
            }
        }

        private void button4_Click_2(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            server.SetDir(folderBrowserDialog1.SelectedPath);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string h = "hello mate!";
            byte[] data = Encoding.ASCII.GetBytes(h);
            server.SendData(data, 0, server.clients[0]);
        }
    }
}
